package cn.hs.lyxf.web.controller;

import cn.hs.lyxf.domain.UserCompany;
import cn.hs.lyxf.query.UserCompanyQuery;
import cn.hs.lyxf.result.JsonResult;
import cn.hs.lyxf.result.PageList;
import cn.hs.lyxf.service.IUserCompanyService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/userCompany")
public class UserCompanyController {

    @Autowired
    public IUserCompanyService userCompanyService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody UserCompany userCompany) {
        if (userCompany.getId() != null) {
            userCompanyService.updateById(userCompany);
        } else {
            userCompanyService.insert(userCompany);
        }
        return JsonResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id) {
        userCompanyService.deleteById(id);
        return JsonResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public UserCompany get(@PathVariable("id") Long id) {
        return userCompanyService.selectById(id);
    }

    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JsonResult list() {
        return JsonResult.success(userCompanyService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JsonResult page(@RequestBody UserCompanyQuery query) {
        Page<UserCompany> page = new Page<UserCompany>(query.getPage(), query.getRows());
        page = userCompanyService.selectPage(page);
        return JsonResult.success(new PageList<UserCompany>(page.getTotal(), page.getRecords()));
    }

    /**
     * 根据userid查询公司信息
     */
    @RequestMapping(value = "/getByUserId/{userId}", method = RequestMethod.GET)
    public UserCompany getByUserId(@PathVariable("userId") Long userId) {
        Wrapper<UserCompany> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", userId);
        UserCompany userCompany = userCompanyService.selectOne(wrapper);
        return userCompany;
    }
}
